part of '../repository.dart';

const _kAccessTokenExpirationTimeKey = 'expiration_time';
const _kAccessTokenDataKey = 'data';

class _AccessTokenDto extends Equatable {
  const _AccessTokenDto({
    required this.expirationTimeDto,
    required this.data,
  });

  final _TimeDto expirationTimeDto;
  final String data;

  @override
  List<Object?> get props {
    return [
      expirationTimeDto,
      data,
    ];
  }

  factory _AccessTokenDto.fromJson(Map<String, dynamic> json) {
    final expirationTimeString = json[_kAccessTokenExpirationTimeKey] as String;
    final expirationTimeDto = _TimeDto.fromString(expirationTimeString);

    return _AccessTokenDto(
      expirationTimeDto: expirationTimeDto,
      data: json[_kAccessTokenDataKey],
    );
  }

  factory _AccessTokenDto.fromEntity(AccessToken accessToken) {
    final expirationTimeDto = _TimeDto.fromEntity(accessToken.expirationTime);

    return _AccessTokenDto(
      expirationTimeDto: expirationTimeDto,
      data: accessToken.data,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      _kAccessTokenExpirationTimeKey: expirationTimeDto.toString(),
      _kAccessTokenDataKey: data,
    };
  }

  AccessToken toEntity() {
    return AccessToken(
      expirationTime: expirationTimeDto.toEntity(),
      data: data,
    );
  }
}
